using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_SanPhamDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            LoadDropDownList();
            LoadData();
        }
    }
    protected void LoadDropDownList()
    {
        tbSanPhamCollection spCollection = tbSanPhamBUS.GetAll();
        ddl_masp.DataTextField = tbSanPhamColumns.MaSP.ToString();
        ddl_masp.DataValueField = tbSanPhamColumns.MaSP.ToString();
        ddl_masp.DataSource = spCollection;
        ddl_masp.DataBind();
        ddl_masp.Items.Insert(0, new ListItem("All", "0"));
        
        tbDanhMucCollection dmCollection = tbDanhMucBUS.GetAll();
        ddl_danhmuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_danhmuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_danhmuc.DataSource = dmCollection;
        ddl_danhmuc.DataBind();
        ddl_danhmuc.Items.Insert(0, new ListItem("All", "0"));
      
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();

        if (txt_tensp.Text != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.TenSP, Condition.LIKE, txt_tensp.Text);
        }
        if (ddl_masp.SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.MaSP, Condition.LIKE, ddl_masp.SelectedValue);
        }
       
        if (ddl_danhmuc.SelectedValue != "0")
        {
            c.add(Condition.AND, tbSanPhamColumns.DanhMuc, Condition.LIKE, ddl_danhmuc.SelectedValue);
        }
        int total = 0;
        tbSanPhamCollection spCollection = tbSanPhamBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbSanPhamColumns.MaSP.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = spCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            tbSanPham sanpham = (tbSanPham)e.Item.DataItem;
            HyperLink hyp_TenSP = (HyperLink)e.Item.FindControl("hyp_TenSP");
            Literal lit_DanhMuc = (Literal)e.Item.FindControl("lit_DanhMuc");
            tbDanhMuc danhMuc = tbDanhMucBUS.GetByID(sanpham.DanhMuc);
            lit_DanhMuc.Text = danhMuc.TenDanhMuc;

            Image img = (Image)e.Item.FindControl("hinhanh");

            int IDH = tbHinhAnhBUS.imageFirst(int.Parse(sanpham.MaSP.ToString()));
            tbHinhAnh  hh = tbHinhAnhBUS.GetByID(IDH);
            img.ImageUrl = "~/Admin/Photos/Thumb/" + hh.HinhAnh.ToString();

            //Literal lit_GiaBan = (Literal)e.Item.FindControl("lit_GiaBan");
            //lit_GiaBan.Text =int.Parse ( sanpham.GiaBan);
            //Literal lit_SoLuongToiDa = (Literal)e.Item.FindControl("lit_SoLuongToiDa");
            //lit_SoLuongToiDa.Text = int.Parse (sanpham.SoLuongBan);
            Literal lit_NgayBatDau = (Literal)e.Item.FindControl("lit_NgayBatDau");
            
            

            hyp_TenSP.Text = sanpham.TenSP;
            hyp_TenSP.NavigateUrl = "~/Admin/SanPhamChiTiet.aspx?iD=" + sanpham.MaSP.ToString();

            

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/SanPhamUpdate.aspx?iD=" + sanpham.MaSP.ToString();
           
        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbSanPham sanpham = tbSanPhamBUS.GetByID(iD);
                
                tbSanPhamBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
}
